package com.cn.demo4.servlet.admin.nav;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.model.NavModel;
import com.cn.demo4.model.PageModel;
import com.cn.demo4.service.INavService;
import com.cn.demo4.service.impl.NavServiceImpl;
import com.cn.demo4.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/nav/findAll")
public class NavFindAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解决乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        //得到前台传来的数据(模糊查询)
        String navName = req.getParameter("navName");
        String navType = req.getParameter("navType");
        String href = req.getParameter("href");
        String status = req.getParameter("status");
        //判断status是否为null，是null则为“-1”代表全部
        status = status == null ? "-1" : status;
        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");

        //封装数据
        NavModel navModel = new NavModel();
        navModel.setName(navName);
        navModel.setHref(href);
        navModel.setStatus(Tool.stringToInt(status));
        navModel.setTypeName(navType);
        //分页对象
        PageModel pageModel = new PageModel();
        pageModel.setPageSize(Tool.toPageSize(pageSize));
        pageModel.setCurPage(Tool.toCurPage(curPage));
        navModel.setPageModel(pageModel);

        //2.调用服务逻辑层
        INavService iNavService = new NavServiceImpl();
        //3.定义一个空集合用来存放查询结果
        List<NavModel> list = iNavService.findAll(navModel);
        int count = iNavService.getCount(navModel);

        //4.将查询结果返回给前台
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);

    }
}
